package com.bz.sunmipay.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.baizhi.baseapp.activity.BaseActivity;
import com.bz.sunmipay.R;
import com.bz.sunmipay.controller.GlobalDataHolder;
import com.bz.sunmipay.model.UserInfo;
import com.bz.sunmipay.utils.IntentUtils;

import java.util.UUID;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText login_et_name;
    private EditText login_et_pwd;
    private Button mLoginBtn;
    private LinearLayout container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_login);
        login_et_name = findViewById(R.id.login_et_name);
        login_et_pwd = findViewById(R.id.login_et_pwd);
        mLoginBtn = findViewById(R.id.login_btn_login);
        container = findViewById(R.id.login_ll_container);
        initListener();
        login_et_name.setText(getRecordUserName());
    }

    private void initListener() {
        mLoginBtn.setOnClickListener(this);
        //监听键盘弹出/隐藏
        container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                //getWindowVisibleDisplayFrame 获取当前窗口可视区域大小
                getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
                int screenHeight = getWindow().getDecorView().getHeight();
                //键盘弹出时，可视区域大小改变，屏幕高度 - 窗口可视区域高度 = 键盘弹出高度
                int softHeight = screenHeight - rect.bottom;
                /**
                 * 上移的距离 = 键盘的高度 - 按钮距离屏幕底部的高度(如果手机高度很大，上移的距离会是负数，界面将不会上移)
                 * 按钮距离屏幕底部的高度是用屏幕高度 - 按钮底部距离父布局顶部的高度
                 * 注意这里 btn.getBottom() 是按钮底部距离父布局顶部的高度，这里也就是距离最外层布局顶部高度
                 */
                int scrollDistance = softHeight - (screenHeight - mLoginBtn.getBottom());
                if (scrollDistance > 0) {
                    //具体移动距离可自行调整
                    container.scrollTo(0, scrollDistance + 60);
                } else {
                    //键盘隐藏，页面复位
                    container.scrollTo(0, 0);
                }
            }
        });
    }

    @Override
    protected boolean isShowTitle() {
        return false;
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        if (id == R.id.login_btn_login) {
            login();
        }
    }

    private void login() {
        final String name = login_et_name.getText().toString().trim();
        final String pwd = login_et_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            showMessage(getResources().getString(R.string.login_name_error));
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            showMessage(getResources().getString(R.string.login_password_error));
            return;
        }
        final UserInfo userInfo = new UserInfo();
        String uid = UUID.randomUUID().toString();
        userInfo.setNickname(name);
        userInfo.setPassword(pwd);
        userInfo.setUid(uid);
        GlobalDataHolder.getInstance(this).setUserInfo(userInfo);
        IntentUtils.goToHome(this);
        recordUseName(name);
        finish();
    }

    private void recordUseName(String userName){
        //步骤1：创建一个SharedPreferences对象
        SharedPreferences sharedPreferences= getSharedPreferences("login", Context.MODE_PRIVATE);
        //步骤2： 实例化SharedPreferences.Editor对象
        SharedPreferences.Editor editor = sharedPreferences.edit();
        //步骤3：将获取过来的值放入文件
        editor.putString("name", userName);
        //步骤4：提交
        editor.apply();
    }

    private String getRecordUserName(){
        SharedPreferences sharedPreferences= getSharedPreferences("login", Context .MODE_PRIVATE);
        String userId=sharedPreferences.getString("name","");
        return userId;
    }
}
